package com.edu.zju.scs.panacea.controller.plan;

import com.edu.zju.scs.panacea.entity.BaseResult;
import com.edu.zju.scs.panacea.entity.plan.Plan;
import com.edu.zju.scs.panacea.entity.plan.Seller;
import com.edu.zju.scs.panacea.entity.query.PlanQuery;
import com.edu.zju.scs.panacea.service.plan.PlanService;
import com.edu.zju.scs.panacea.utils.DateUtil;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Author: Yue Liang
 * @Date: 2019-01-03 19:46
 */
@Controller
@EnableAutoConfiguration
public class PlanController {
    @Autowired
    private PlanService planService;

    @ResponseBody
    @ApiOperation(value="根据id查询某个计划", notes="根据id查询某个计划")
    @RequestMapping(value = "/plan/info/{id}", method = RequestMethod.GET)
    public BaseResult getPlan(@PathVariable Long id) {
        return planService.getPlan(id);
    }

    @ResponseBody
    @ApiOperation(value="查询登录商家的所有计划", notes="查询登录商家的所有计划")
    @RequestMapping(value = "/plan/seller", method = RequestMethod.POST)
    public BaseResult getPlanBySeller(HttpSession session) {
        Seller seller = (Seller) session.getAttribute("seller");
        PlanQuery planQuery = new PlanQuery();
        planQuery.setSellerId(seller.getId());
        return planService.getPlanBySeller(planQuery);
    }

    @ResponseBody
    @ApiOperation(value="根据ID删除计划", notes="根据ID删除计划")
    @RequestMapping(value = "/plan/deletion/{id}", method = RequestMethod.GET)
    public BaseResult deletePlan(@PathVariable Long id) {
        return planService.deletePlan(id);
    }

    @ResponseBody
    @ApiOperation(value="新增计划", notes="新增计划")
    @RequestMapping(value = "/plan/new", method = RequestMethod.POST)
    public BaseResult addPlan(
            @ApiParam("计划名称") @RequestParam(value = "name") String name,
            @ApiParam("行业ID") @RequestParam(value = "industryId") Long industryId,
            @ApiParam("类型") @RequestParam(value = "type") Integer type,
            @ApiParam("资源位") @RequestParam(value = "locIds") List<String> locIds,
            @ApiParam("资源包") @RequestParam(value = "packageIds") List<String> packageIds,
            @ApiParam("开始日期") @RequestParam(value = "startDate") String startDate,
            @ApiParam("结束日期") @RequestParam(value = "endDate") String endDate,
            @ApiParam("每日投放时间") @RequestParam(value = "launchTime") List<Integer> launchTime,
            @ApiParam("素材模板ID") @RequestParam(value = "templateIds") List<Long> templateIds,
            @ApiParam("素材URL") @RequestParam(value = "materialUrls") List<String> materialUrls,
            @ApiParam("素材播放的时间") @RequestParam(value = "materialSeconds") List<Integer> materialSeconds,
            HttpSession session) {
        Seller seller = (Seller) session.getAttribute("seller");
        Plan plan = new Plan();
        plan.setIndustryId(industryId);
        plan.setType(type);
        plan.setLocIds(locIds);
        plan.setPackageIds(packageIds);
        plan.setName(name);
        plan.setSellerId(seller.getId());
        plan.setStartDate(DateUtil.toSqlDate(startDate));
        plan.setEndDate(DateUtil.toSqlDate(endDate));
        plan.setLaunchTime(launchTime);
        return planService.addPlan(plan, templateIds, materialUrls, materialSeconds);
    }


    @ResponseBody
    @ApiOperation(value="修改计划信息", notes="修改计划信息")
    @RequestMapping(value = "/plan/edit", method = RequestMethod.POST)
    public BaseResult editPlan(
            @ApiParam("计划ID") @RequestParam(value = "id") Long id,
            @ApiParam("行业ID") @RequestParam(value = "industryId") Long industryId,
            @ApiParam("类型") @RequestParam(value = "type") Integer type,
            @ApiParam("计划名称") @RequestParam(value = "name") String name,
            @ApiParam("资源位") @RequestParam(value = "locIds", required = false) List<String> locIds,
            @ApiParam("资源包") @RequestParam(value = "packageIds", required = false) List<String> packageIds,
            @ApiParam("开始日期") @RequestParam(value = "startDate") String startDate,
            @ApiParam("结束日期") @RequestParam(value = "endDate") String endDate,
            @ApiParam("每日投放时间") @RequestParam(value = "launchTime") List<Integer> launchTime,
            @ApiParam("状态") @RequestParam(value = "state") Integer state, HttpSession session) {
        Seller seller = (Seller) session.getAttribute("seller");
        Plan plan = new Plan();
        plan.setIndustryId(industryId);
        plan.setType(type);
        plan.setId(id);
        plan.setLocIds(locIds);
        plan.setPackageIds(packageIds);
        plan.setName(name);
        plan.setSellerId(seller.getId());
        plan.setStartDate(DateUtil.toSqlDate(startDate));
        plan.setEndDate(DateUtil.toSqlDate(endDate));
        plan.setLaunchTime(launchTime);
        plan.setState(state);
        return planService.editPlan(plan);
    }

    @ResponseBody
    @ApiOperation(value="查询某个计划的失败原因", notes="查询某个计划的失败原因")
    @RequestMapping(value = "/plan/audit/reason/{id}", method = RequestMethod.GET)
    public BaseResult getPlanAuditResult(@PathVariable Long id) {
        return planService.getAuditResult(id);
    }

}
